/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lczyfz.wisdom.modules.platform.service;

import com.lczyfz.wisdom.common.persistence.Page;
import com.lczyfz.wisdom.common.service.CrudService;
import com.lczyfz.wisdom.common.utils.ResponseCode;
import com.lczyfz.wisdom.common.utils.ServerResponse;
import com.lczyfz.wisdom.common.utils.StringUtils;
import com.lczyfz.wisdom.modules.platform.dao.OrderDao;
import com.lczyfz.wisdom.modules.platform.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单Service
 * @author css
 * @version 2019-09-26
 */
@Service
@Transactional(readOnly = true)
public class OrderService extends CrudService<OrderDao, Order> {

	@Autowired
    OrderDao orderDao;

	public Order get(String id) {
		return super.get(id);
	}
	
	public List<Order> findList(Order order) {
		return super.findList(order);
	}
	
	public Page<Order> findPage(Page<Order> page, Order order) {
		return super.findPage(page, order);
	}
	
	@Transactional(readOnly = false)
	public void save(Order order) {
		super.save(order);
	}

	@Transactional(readOnly = false)
	public ServerResponse<Object> addOrder(Order order) {
		ServerResponse<Object> serviceResult = new ServerResponse<Object>(0,"预约成功");
		try {
			if(null == order.getVisitdate() || "".equals(order.getVisitdate())){
				return new ServerResponse(-10000,"上门服务时间不能为空");
			}
			if(StringUtils.isBlank(order.getAddressId())){
				return new ServerResponse(-10001,"请选择有效的地址信息");
			}
			if(StringUtils.isBlank(order.getUserId())){
				return new ServerResponse(-10002,"用户不能为空");
			}
			if(StringUtils.isBlank(order.getTitle())){
				return new ServerResponse(-10003,"预约主题不能为空");
			}
			order.setState("1");
			super.save(order);
		}catch (Exception e){
			e.printStackTrace();
			return new ServerResponse<Object>(-10004,"预约失败");
		}
		return serviceResult;
	}

	public ServerResponse<Order> getList(String userId){
		ServerResponse<Order> serviceResult = new ServerResponse<Order>(0,"获取预约列表成功");
		try {
			List<Order> list = orderDao.getAllByUserId(userId);
			if (list.size()>0){
				serviceResult.setDataList(list);
			}else {
				return new ServerResponse<Order>(0,"当前预约列表为空");
			}
		}catch (Exception e){
			e.printStackTrace();
			return new ServerResponse<Order>(0,"获取预约;列表失败");
		}
		return serviceResult;
	}
	
	@Transactional(readOnly = false)
	public void delete(Order order) {
		super.delete(order);
	}

	//修改预约订单状态
	@Transactional(readOnly = false)
	public void updateState(String userId) {
		orderDao.updateState(userId);
	}
	
}